package LocalDNSServer2;

import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.util.List;

public class ListenThread implements Runnable{

    DatagramSocket socket = null;
    DatagramPacket packet = null;

    public ListenThread(DatagramSocket socket, DatagramPacket packet) {
        this.socket = socket;
        this.packet = packet;
    }

    @Override
    public void run() {
        String info = null;
        DatagramPacket packet2 = null;
        try {
            info = new String(packet.getData(), 0, packet.getLength());

            System.out.println(info);   //接收到的消息
            if (info.split(",")[0].equals("1") ){   //第一个字符是1，代表是查询请求
                int portReq = Integer.parseInt(info.split(",")[2]) ;
                String website = info.split(",")[1];   //获取要解析的域名
                String a = ShareSpaceAndFun.checkBuf(website);  //查缓存看看又没有，返回a 如果有返回的是一个IP，否则是NULL
                if (a == null){   //说明没有
                    System.out.println("没有缓存");

                    if (ShareSpaceAndFun.addWait(website,portReq)){
                        //转发根服务器
                        InetAddress address =  InetAddress.getByName("localhost");
                        String f = "2,";   //迭代式
                        if (ShareSpaceAndFun.getTypeOfCheck()){
                            f = "1,";  //递归式
                        }
                        byte[] data2 = (f +website+","+ShareSpaceAndFun.myPort).getBytes();
                        packet2 = new DatagramPacket(data2, data2.length, address, 4060);
                        socket.send(packet2);
                    }

                }else {   //有缓存
                    System.out.println("缓存为" + a);
                    //相应请求
                    InetAddress address =  InetAddress.getByName("localhost");
                    byte[] data2 = (website+","+a).getBytes();
                    packet2 = new DatagramPacket(data2, data2.length, address, Integer.parseInt(info.split(",")[2]));
                    socket.send(packet2);
                }
            }else if (info.split(",")[0].equals("0") ){   //第一个字符是0，代表是查询结果
                String website = info.split(",")[1];   //获取要解析的域名
                String a = info.split(",")[2];

                InetAddress address = null;
                address = InetAddress.getByName("localhost");

                // 更新缓存表
                ShareSpaceAndFun.updateBuf(website,a);

                List<Integer> portWaitList = ShareSpaceAndFun.getWaitPortByWebsite(website);
                if (portWaitList != null) {
                    for (Integer port : portWaitList) {
                        //发回客户端
                        byte[] data2 = (website + "," + a).getBytes();
                        packet2 = new DatagramPacket(data2, data2.length, address, port);
                        socket.send(packet2);
                    }
                }

            }else if (info.split(",")[0].equals("2") ){   //第一个字符是2，代表是间接查询结果
                String website = info.split(",")[1];   //获取要解析的域名
                String portNext = info.split(",")[2];
                //再次转发查询
                InetAddress address =  InetAddress.getByName("localhost");
                byte[] data2 = ( "2," +website+","+ShareSpaceAndFun.myPort).getBytes();
                packet2 = new DatagramPacket(data2, data2.length, address, Integer.parseInt(portNext));
                socket.send(packet2);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

}